package cc.idoone.spring_boot_hello.mybatis.h2.dao;

import cc.idoone.spring_boot_hello.mybatis.h2.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @auther hemj
 * @since 2017/9/25
 */
public interface UserMapper {

	String TABLE = "CREATE TABLE IF NOT EXISTS user(\n" +
			"  id INTEGER not null primary key auto_increment,\n" +
			"  name char(20),\n" +
			"  age INTEGER\n" +
			")";

	@Update(TABLE)
	public void createTable();

	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") //回写自增的主键ID，没效果（fuck）
	@Insert("insert into user (name,age) values (#{name},#{age})")
	public Integer addUser(User user);

	@Delete("delete from user where id=#{0}")
	public Integer deleteUserById(Integer id);

	@Update("update user set name=#{name},age=#{age} where id=#{id}")
	public Integer updateUser(User user);

	@Select("select id,name,age from user where id=#{0}")
	@Results(value = {
			@Result(id = true, column = "id", property = "id"),
			@Result(column = "name", property = "name"),
			@Result(column = "age", property = "age")})
	public User getById(Integer id);

	@Select("select id,name,age from user")
	public List<User> queryUserList();
}
